package com.net.oil.user;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.app.UserInfoManager;
import com.net.oil.base.BaseActivity;
import com.net.oil.base.EventBussMessage;
import com.net.oil.user.contract.DeleteAccountContract;
import com.net.oil.user.presenter.DeleteAccountPresenter;
import com.net.oil.util.StringUtil;
import com.net.oil.widget.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class DeleteAccountActivity extends BaseActivity<DeleteAccountPresenter> implements DeleteAccountContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.phone_number_et)
    EditText phoneNumberEt;
    @BindView(R.id.password_et)
    EditText passwordEt;
    @BindView(R.id.get_verify_code_button)
    TextView getVerifyCodeButton;
    @BindView(R.id.verify_code_et)
    EditText verifyCodeEt;
    @BindView(R.id.delete_button)
    Button deleteButton;

    private long allTime = 60 * 1000;   //计时器时间
    private long stepTime = 1000;       //计时器每步的时间

    //计时器
    CountDownTimer timer = new CountDownTimer(allTime, stepTime) {
        @Override
        public void onTick(long millisUntilFinished) {
            getVerifyCodeButton.setText(String.format(getString(R.string.millis_format), millisUntilFinished / 1000));
            getVerifyCodeButton.setEnabled(false);
        }

        @Override
        public void onFinish() {
            getVerifyCodeButton.setText("重新获取验证码");
            getVerifyCodeButton.setEnabled(true);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_account);
        ButterKnife.bind(this);
        presenter = new DeleteAccountPresenter(this);
        initView();
    }

    private void initView() {
        toolbarTitle.setText("注销账号");

    }

    /**
     * 获取验证码成功
     */
    @Override
    public void getPhoneCodeSuccess() {

    }

    /**
     * 获取验证码失败
     */
    @Override
    public void getPhoneCodeFailed() {

    }

    /**
     * 删除账号成功
     */
    @Override
    public void deleteAccountSuccess() {
        dismissProgressDialog();
        UserInfoManager.getInstance().clearUserInfo();
        EventBus.getDefault().post(new EventBussMessage(Constant.LOGOUT_CODE, Constant.LOGOUT_MESSAGE));
        finish();
    }

    /**
     * 删除账号失败
     */
    @Override
    public void deleteAccountFailed() {
        dismissProgressDialog();
    }

    @OnClick({R.id.toolbar_go_back, R.id.get_verify_code_button, R.id.delete_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
            case R.id.get_verify_code_button:
                getPhoneCode();
                break;
            case R.id.delete_button:
                deleteAccount();
                break;
        }
    }

    /**
     * 获取验证码
     */
    public void getPhoneCode() {
        String phoneNumber = phoneNumberEt.getText().toString();
        if (!StringUtil.isEmpty(phoneNumber)) {
            timer.start();
            presenter.getPhoneCode(phoneNumber, false);
        }else {
            ToastUtil.showErrorToast(this, "请输入手机号");
        }
    }

    private void deleteAccount(){
        String phoneNumber = phoneNumberEt.getText().toString();
        String password = passwordEt.getText().toString();
        String code = verifyCodeEt.getText().toString();

        if (TextUtils.isEmpty(phoneNumber)){
            ToastUtil.showErrorToast(this, "请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(password)){
            ToastUtil.showErrorToast(this, "请输入密码");
            return;
        }
        if (TextUtils.isEmpty(code)){
            ToastUtil.showErrorToast(this, "请输入验证码");
            return;
        }
        showDialog(phoneNumber, password, code);
    }

    private void showDialog(final String phoneNumber, final String password, final String code){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("注销账号会清除该账号的所有数据，并且不能恢复，你确定注销账号吗？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showProgressDialog();
                presenter.deleteAccount(phoneNumber, password, code);
            }
        });
        builder.show();
    }
}
